//全局变量
var pId=-1;//诗的id
var appH=0;//画布高度
var url='AllPoem';//加载更多地址
var n=900;//特效速度
var times=0;//保证同一首诗只能成功入库一次
//获取长度最小的列表
function getMinHeightCol(){
	var minHeight;
	var indexOfMin;
	$(".col").each(function(index, element) {
		if($(element).outerHeight()>=minHeight) return;
		minHeight=$(element).outerHeight();
		indexOfMin=index;	
        });
	//alert("最小长度为："+minHeight+"索引为："+indexOfMin);
	return indexOfMin;
	}
//调整画布高度
function resizeH(){
	var bodyH=$("#poemApp").outerHeight();
	if(appH!=bodyH){
		appH=bodyH;
		fusion2.canvas.setHeight({
			height : appH
			});
	}
}

//更多的诗
function more(){
	$.post(url,{pId:pId},function(data){
		//alert(1);
		    var dataObj=eval("("+data+")"); 
		    if(dataObj.length==0){
		    	$("#morePoems").hide(n,function(){resizeH();});
		    	return;
		    	};
		    for(var i=0;i<dataObj.length;i++){
		    	pId=dataObj[i].id;
		    	var poem='<div class="titleFont">'+dataObj[i].poemname+'</div><div class="authorFont">'
					+dataObj[i].author+'</div><div class="contentFont">'+dataObj[i].content+'</div>';
		    	var content='<div id="poem'+pId+'" class="col-s1" style="display:none;"><div class="poem col-s2" title="五言绝句">'+poem+
		        '<br /></div></div>';
		    	$(".col").eq(getMinHeightCol()).append(content);
		    	$("#poem"+pId).fadeIn(n);
		    	resizeH();
		    }
		});
	}
//初始化
function initial(){
	pId=-1;
	//清空诗列表
	$(".col").each(function(){
		$(this).html("");
	});
	$("#morePoems").show(n,function(){resizeH();});
	more();
}
//网页加载完成
$(document).ready(function(e) {
	//初次需要显示提示
	var tipVisible = GetCookie("tipVisible");
	var neverShow = GetCookie("neverShow");
	if (tipVisible != "no" && neverShow != "no") {
		showSearchTip();
		setSearchTip();
	}
	//初始化，默认显示全部的诗
	$("#allpoems").attr("disabled","disabled");
	$("#mypoems").attr("disabled","");
	$("#morePoems").attr("disabled","");
	initial();
	//发布区
     $('#show').hide(n/2);
	 $("#mypoem").hide(n);
	 $("#poemmsg1").hide(n);
	 $("#poemmsg2").hide(n,function(){resizeH();});
	 //开始按钮
	 $("#startbtn").attr("disabled","");
	 $("#startbtn").click(function(){
		$(this).attr("disabled","disabled");
		var title = $("#title").val();
		title=title.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
		var keyword = $("#keyword").val();
		if(title == "" || keyword == "") {
			$("#poem").show(n,function(){resizeH();});
			$("#poemmsg1").html("标题或关键字信息不能为空").slideDown(n/2);
			$("#startbtn").attr("disabled","");
			return;
		};

		//加载中loading
		$("#AjaxLoading").css({"display":"inline"});
		$(".showbox").show();
		$(".showbox").stop(true).animate({'margin-top':'150px','opacity':'1'},200);
		setTimeout(function(){
				$(".showbox").stop(true).animate({'margin-top':'150px','opacity':'0'},400);
				},1000*1000);
		//生成古诗
		$.post("createPoem.jsp",{title:title,keyword:keyword},function(data){
			data=data.replace(/(^\s*)|(\s*$)/g, "");//去首尾空格
			//alert(data+"，诗歌的文本长度："+data.length);//////////////////////
			if(data.length<23){
				$("#sendbtn").attr("disabled","disabled");
				$("#poemmsg2").html("诗歌生成失败！").slideDown(n,function(){resizeH();});
			}else{
				data=data.replace(/，/g  , "，<br />");
				data=data.replace(/。/g  , "。<br />");
				data='<div id="ptitle">'+title+'</div><div id="pcontent">'+data+'</div>';
				$("#sendbtn").attr("disabled","");
				times=0;
				$("#poemmsg2").html("诗歌生成成功！").slideDown(n);
				$("#edit").hide(n);
			}
			$(".showbox").hide();
			$("#poemmsg1").hide(n);	
			$("#poem").show(n);
			$("#mypoem").html(data).slideDown(n);
			$("#show").slideDown(n,function(){resizeH();});
			$("#startbtn").attr("disabled","");
		});
		
	});
	//返回按钮
	$("#backbtn").click(function(){
		$("#edit").slideDown(n);
		$("#mypoem").hide(n);
		$("#poemmsg2").hide(n);
		$("#show").hide(n,function(){resizeH();});
	});
	//发布按钮
	$("#sendbtn").click(function(){
		$(this).attr("disabled","disabled");
		var title=$("#ptitle").html();
		var content=$("#pcontent").html();
		if(title==""||content=="")return;
		if(times==0){
			times=-1;//防止重复提交
			$.post("poetry/public.jsp",{title:title,content:content},function(data){
				$("#sendbtn").attr("disabled","");
				data=data.replace(/(^\s*)|(\s*$)/g, "");
				if(data=='true'){
					//alert("存储成功");
					times=1;
					//发表日志
					fusion2.dialog.saveBlog({
					    title : title , 
					    content : '<p align="center" style="font-family:宋体; font-size:24px; color:#30F">'+content+'</p>'
//					   context : "blog_12345", 
//					    onSuccess : function (opt) 
//					    { alert("Succeeded: " + opt.context); window.console && console.log(opt);}, 
//					    onCancel : function (opt) 
//					    { alert("Cancelled: " + opt.context); }, 
//					    onClose : function (opt) 
//					    { alert("Closed"); }
					 });
				}else{
					//alert("存储失败");
					times=0;
				}
				//清空输入
				$("#title").val("");
				$("#keyword").val("");
			});
		}else if(times==1){
			//发表日志
			fusion2.dialog.saveBlog({
			    title : title , 
			    content : '<p align="center" style="font-family:宋体; font-size:24px; color:#30F">'
			    	+content+'</p>'
			 });
			$("#sendbtn").attr("disabled","");
		}
		});
		
	//tab切换
	$("#allpoems").click(function(){
		$(this).attr("disabled","disabled");
		$("#mypoems").attr("disabled","");
		$("#friendpoem").attr("disabled","");
		url='AllPoem';
		initial();
	});

	$("#mypoems").click(function(){
		$(this).attr("disabled","disabled");
		$("#allpoems").attr("disabled","");
		$("#friendpoem").attr("disabled","");
		url='MyPoem';
		initial();
	});
	
	$("#friendpoem").click(function(){
		$(this).attr("disabled","disabled");
		$("#allpoems").attr("disabled","");
		$("#mypoems").attr("disabled","");
		url='FriendsPoem';
		initial();
	});
	
	//加载更多的诗
	$("#morePoems").click(function(){
		$("#morePoems").attr("disabled","disabled");
		more();
		$("#morePoems").attr("disabled","");
		});	
	//邀请好友
	$("#invite").click(function(){
		fusion2.dialog.invite
		({
		 // receiver : "00000000000000000000000000009FED",
		  msg  : "想写诗？不知道怎么写？不用绞尽脑汁了，输入关键词就能自动帮你写，还等什么呢？快来试试吧！记得给好评啊，亲！",
		//  img : "http://qzonestyle.gtimg.cn/qzonestyle/act/qzone_app_img/app353_353_75.png",
		//  source : "domain=s4.app12345.qqopenapp.com",
		//  context : "invite",
		  onSuccess : function (opt) {  },
		  onCancel : function (opt) { },
		  onClose : function (opt) {  }
		});
	});

});